package com.lvtn.adsonandroid;

import java.util.List;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.ImageLoadingListener;

import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class CommentRowAdapter extends ArrayAdapter<Item> {
	
	private Activity activity;
	private List<Item> items;
	private Item objBean;
	private int row;
//	private DisplayImageOptions options;
	ImageLoader imageLoader;

	public CommentRowAdapter(Activity act, int resource, List<Item> arrayList) {
		super(act, resource, arrayList);
		this.activity = act;
		this.row = resource;
		this.items = arrayList;

	//	options = new DisplayImageOptions.Builder()
	//			.showStubImage(R.drawable.profile)
	//			.showImageForEmptyUrl(R.drawable.profile).cacheInMemory()
	//			.cacheOnDisc().build();
		imageLoader = ImageLoader.getInstance();

	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View view = convertView;
		ViewHolder holder;
		if (view == null) {
			LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(row, null);

			holder = new ViewHolder();
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}

		if ((items == null) || ((position + 1) > items.size()))
			return view;

		objBean = items.get(position);

		holder.tvUser = (TextView) view.findViewById(R.id.user_name);
		Log.d("111","111");
		holder.tvComment = (TextView) view.findViewById(R.id.comment);
		Log.d("111","222");
		holder.tvDate = (TextView) view.findViewById(R.id.date);
		Log.d("111","333");
		holder.imgView = (ImageView) view.findViewById(R.id.image);
		Log.d("111","444");
		

		
		if (holder.tvUser != null && null != objBean.getUser()
				&& objBean.getUser().trim().length() > 0) {
			Log.d("username",objBean.getUser());
			holder.tvUser.setText(Html.fromHtml(objBean.getUser()));
		}
		if (holder.tvComment != null && null != objBean.getComment()
				&& objBean.getComment().trim().length() > 0) {
			holder.tvComment.setText(Html.fromHtml(objBean.getComment()));
		}
		if (holder.tvDate != null && null != objBean.getPubdate()
				&& objBean.getPubdate().trim().length() > 0) {
			holder.tvDate.setText(Html.fromHtml(objBean.getPubdate()));
		}
		if (holder.imgView != null) {
			if (null != objBean.getLink()
					&& objBean.getLink().trim().length() > 0) {
				imageLoader.init(ImageLoaderConfiguration
						.createDefault(activity));
				imageLoader.displayImage(objBean.getLink(), holder.imgView);
			} else {
				holder.imgView.setImageResource(R.drawable.profile);
			}
		}

		return view;
	}

	public class ViewHolder {

		public TextView  tvComment, tvDate,tvUser;
		private ImageView imgView;
	}

}